/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.scheduling;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DAGOperatorOutput {
    // 算子data多输出
    private List<String> data;
    // 算子model多输出
    private List<String> model;

    public DAGOperatorOutput(){
        this.data = new ArrayList<>();
        this.model = new ArrayList<>();
    }

    public List<String> getData() {
        return data;
    }

    public void setData(List<String> data) {
        this.data = data;
    }

    public List<String> getModel() {
        return model;
    }

    public void setModel(List<String> model) {
        this.model = model;
    }

    // 判断两个算子输出是否相等
    public static boolean equals(DAGOperatorOutput base, DAGOperatorOutput ow) {
        // 检查入参是否合法
        if (base == null && ow == null) {
            return true;
        } else if (base == null && ow != null) {
            return false;
        } else if (base != null && ow == null) {
            return false;
        } else {
            // 计算data多输出是否相同
            boolean match = true;
            if (base.getData() == null && ow.getData() == null) {
                ;
            } else if (base.getData() == null && ow.getData() != null) {
                match = false;
            } else if (base.getData() != null && ow.getData() == null) {
                match = false;
            } else {
                match = base.getData().equals(ow.getData());
            }
            if (match == false) {
                return false;
            }
            // 计算model多输出是否相同
            match = true;
            if (base.getModel() == null && ow.getModel() == null) {
                ;
            } else if (base.getModel() == null && ow.getModel() != null) {
                match = false;
            } else if (base.getModel() != null && ow.getModel() == null) {
                match = false;
            } else {
                match = base.getModel().equals(ow.getModel());
            }
            if (match == false) {
                return false;
            }
            return true;
        }
    }

    public static DAGOperatorOutput fromJson(String json) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        DAGOperatorOutput obj = mapper.readValue(json, DAGOperatorOutput.class);
        return obj;
    }

    // 结构体转换为json字符串
    public String toJson() throws JsonProcessingException {
        Map<String, Object> map = new HashMap<>();
        map.put("data", data);
        map.put("model", model);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(map);
        return json;
    }
}
